<?php
session_start(); 
  include("../../sql/connexionmysql.php");
  include("../../controlleur/utilities.php");
  $_SESSION['admin'] = 1;
  if ($_SESSION['admin'] < 1)
		header("location:/../../index.php");
  
  $idTournamentDraw = $_GET['idTournamentDraw'];
  $idTournamentRoundType = $_GET['idTournamentRoundType'];
  $matchOrder = $_GET['matchOrder'];
  
  $match = mysql_fetch_array(mysql_query(
      'SELECT * FROM tournamentMatch WHERE idTournamentDraw = '. $idTournamentDraw .
      ' AND idTournamentRoundType = ' . $idTournamentRoundType . ' AND matchOrder = '. $matchOrder));
  $score = mysqlQueryToArray(mysql_query('SELECT * FROM tournamentGameScore
      WHERE idTournamentMatch	 = '. $match['idTournamentMatch']));
  $tabIndexPlayer3 = $maxNumSet * 2 + 4;
?>

<div id="editMatchWrapper">
  <div onClick="closeMatchEditor()" class="closeMatch">Close</div>
  <div style="display:hidden;" id="refreshEditor"
      onClick="openModify(<?php echo $idTournamentRoundType; ?>, <?php echo $matchOrder; ?>)"></div>
  <form action="" onsubmit="editMatch();return false;">
    <div>
      Modifier le Match
      <span class="tournamentOrderModify">
        <?php echo $matchOrder ?>
      </span> de 
      <span class="idTournamentRoundModify">
        <?php echo getRoundNameFromId($idTournamentRoundType) ?>
      </span>
    </div> 
    
    <?php 
      for ($i = 1; $i <= 4; $i++) {
    ?>
    Joueur <?php echo $i?> :
      <?php printSelectionPlayer('player' . $i, $match['idPlayer' . $i], 
          $i < 3 ? $i : $tabIndexPlayer3); ?>
      <?php 
        if ($i == 1 || $i == 2) {
      ?>
        <?php 
          for ($set = 1; $set <= $maxNumSet; $set++) {
        ?>
          <?php $currentTabIndex = 2 + $set * 2 + $i ?>
          <input class="setInput" name="player<?php echo $i?>set<?php echo $set ?>"
              maxlength="2" value="<?php echo getScore($i, $set, $score, $match) ?>"
              tabindex="<?php echo $currentTabIndex ?>"/>
        <?php }?>
          KO :
          <input type="checkbox" name="playerKO" value="<?php echo $i?>"
              <?php echo $match['playerKO'] == $i ? 'checked' : '' ?> />
      <?php }?>
      <br/>
    <?php
     }
    ?>
    <script type="text/javascript">
      $('.setInput').keyup(function(e) {
          if (e.which >= 48 && e.which <=57) {
            var tabindex = parseInt($(this).attr('tabindex'));
            $('.setInput[tabindex=' + (tabindex + 1) + ']').focus();
          }
      })
    </script>
    <?php 
    /*
      function getAllFields2() {
        $getAllFieldsSql = 'SELECT DISTINCT * FROM tournamentField ORDER BY fieldName';
        return mysql_query($getAllFieldsSql);
      }
      $allFields = getAllFields2();*/
    $allFields = mysql_query('SELECT DISTINCT * FROM tournamentField ORDER BY fieldName');
    
    ?>
    Terrain:
    <select name="field" tabindex="<?php echo $tabIndexPlayer3++; ?>">
      <option value="0" class="option0">
        ??
      </option>
      <?php 
        while ($field = mysql_fetch_array($allFields)) {
      ?>
        <option value="<?php echo $field['idTournamentField'] ?>"
            class="option<?php echo $field['idTournamentField'] ?>"
            <?php echo $match['idTournamentField'] == $field['idTournamentField'] ? 'selected' : ''; ?>
            >
          <?php echo $field['fieldName'] ?>
        </option>
      <?php
        }
      ?>  
    <select/>
    <br/>
    Date:
    <input type="date" name="date" value="<?php echo $match['date']?>"
        tabindex="<?php echo $tabIndexPlayer3 ++; ?>"/>
    <br/>
    Heure:
    <select name="hour" tabindex="<?php echo $tabIndexPlayer3 ++; ?>"
        class="timingSelect">
         <!-- 
      <?php 
        for ($hour = 8; $hour < 20; $hour += 1) {
        ?>
        <option value="<?php echo $hour ?>"
            <?php echo $match['hour'] == $hour ? 'selected' : ''; ?>
            >
          <?php echo ($hour >= 10 ? '' : '0') . $hour ?>
        </option>
        <?php 
        }
      ?>
         -->
         <option value="10" <?php echo $match['hour'] == 10 ? 'selected' : ''; ?>>10</option>
         <option value="14" <?php echo $match['hour'] == 14 ? 'selected' : ''; ?>>14</option>
         <option value="16" <?php echo $match['hour'] == 16 ? 'selected' : ''; ?>>16</option>
         <option value="17" <?php echo $match['hour'] == 17 ? 'selected' : ''; ?>>17</option>
         <option value="18" <?php echo $match['hour'] == 18 ? 'selected' : ''; ?>>18</option>
         <option value="19" <?php echo $match['hour'] == 19 ? 'selected' : ''; ?>>19</option>
    </select>
    :
    <select name="minute" tabindex="<?php echo $tabIndexPlayer3 ++; ?>"
        class="timingSelect">
        <!-- 
      <?php 
        for ($minute = 0; $minute < 60; $minute += 5) {
        ?>
        <option value="<?php echo $minute ?>"
            <?php echo $match['minute'] == $minute ? 'selected' : ''; ?>
            >
          <?php echo ($minute >= 10 ? '' : '0') . $minute ?>
        </option>
        <?php 
        }
      ?>
         -->
         <option value="00" <?php echo $match['minute'] == 0 ? 'selected' : ''; ?>>00</option>
         <option value="30" <?php echo $match['minute'] == 30 ? 'selected' : ''; ?>>30</option>
    </select>
    
    
    
    <input type="hidden" name="idTournamentMatch" value="<?php echo $match['idTournamentMatch']?>"/>
    <br/><br/>
    <div class="editMatchButtonWrapper">
      <input type="submit" tabindex="<?php echo $tabIndexPlayer3 + 1; ?>" value="Modifier le Match"/>
    </div>
    
  </form>
  <div id="outputEditMatch"></div>
</div>



